package com.example.Wisdompark.controller;


import com.example.Wisdompark.pojo.*;
import com.example.Wisdompark.service.*;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 订单处理，包括入库订单、出库订单、退货订单等。
 */
@Controller
public class OrderController {

    Gson gson = new Gson();
    @Autowired
    private EntryOrderService entryOrderService;
    @Autowired
    private OutOrderService outOrderService;
    @Autowired
    private ReturnOrderService returnOrderService;
    @Autowired
    private CurrentCargoService currentCargoService;
    @Autowired
    private WarehouseService warehouseService;

    /*管理人员部分*/
    // 入库订单

    // 展示入库订单
    @RequestMapping("/Entry")
    public String PageToEntry(Model model) {
        // 查询所有未审核的入库订单
        List<Entry_Order> entry_orders = entryOrderService.queryAllOrder();
        model.addAttribute("entryoutOrders", entry_orders);
        // 跳转到入库订单展示页面
        return "managers/entryOrder";
    }

    // 不查询直接跳转到入库订单展示页面
    @RequestMapping("/EntryOrder")
    public String EntryOrder() {
        return "managers/entryOrder";
    }


    // 展示入库订单具体内容
    @RequestMapping("/entryOrderContent")
    public String queryOrderContent(
            @Param("order_num") String order_num,
            Model model) {
        // 通过订单号查询入库订单
        Entry_Order entry_order = entryOrderService.queryByOrderNum(order_num);
        model.addAttribute("entryOrder", entry_order);
        // 跳转到订单具体内容展示页面
        return "managers/order";
    }

    // 批准入库订单 处理流程
    @RequestMapping("/approve")
    public String approveEntryOrder(
            @Param("order_num") String order_num,
            Model model
    ) {

        // 首先查询是否有空货位
        Integer firstEmptyPlace = warehouseService.getFirstEmptyPlace();

        // 有空位时
        if (firstEmptyPlace != null) {

            // 根据订单号查询货物
            List<Current_Cargo> current_cargos = currentCargoService.queryByOrderNum(order_num);

            // 获取到第一个
            Current_Cargo current_cargo = current_cargos.get(0);

            // 创建一个warehouse对象，并设置它的各个属性
            Warehouse warehouse = new Warehouse();
            // 设置货位
            warehouse.setPlace_id(firstEmptyPlace);
            // 设置货物名称
            warehouse.setCargo_name(current_cargo.getCargo_name());
            // 设置货物数量
            warehouse.setCargo_amount(current_cargo.getCargo_amount());
            // 更新仓库中相应货位的记录
            warehouseService.updateWarehouse(warehouse);

            // 订单状态转为通过
            entryOrderService.updateOrderState(order_num, 1);
            // 货物状态转变为入库
            currentCargoService.updateCargoState(order_num, 1);


        }

        // 查询所有未审核的入库订单
        List<Entry_Order> entry_orders = entryOrderService.queryAllOrder();
        model.addAttribute("entryoutOrders", entry_orders);
        // 跳转到入库订单展示页面
        return "managers/entryOrder";
    }


    // 出库订单 展示
    @RequestMapping("/Out")
    public String PageToOut(Model model) {
        // 查询状态是未审核的所有出库订单
        List<Out_Order> out_orders = outOrderService.queryAllOrder();
        model.addAttribute("entryoutOrders", out_orders);
        // 跳转到出库订单展示页面
        return "managers/outOrder";
    }

    //  出库订单具体内容展示
    @RequestMapping("/outOrderContent")
    public String outOrderContent(
            @Param("order_num") String order_num,
            Model model) {
        // 根据前端传来的出库订单号，查询相应的订单
        Out_Order out_order = outOrderService.queryByOrderNum(order_num);
        model.addAttribute("outOrder", out_order);
        return "managers/outOrder";
    }

    // 地图页面请求详细信息,转换为json,然后再传过去
    @RequestMapping("/outOrderContent_map")
    public void outOrderContent_map(
            @Param("order_num")String order_num,
            HttpServletResponse response
    ){
        Out_Order out_order = outOrderService.queryByOrderNum(order_num);
        String s = gson.toJson(out_order);
        try {
            response.getWriter().write(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 出库订单批准 处理流程
     *
     * @param order_num    出库订单号
     * @param cargo_name   货物名称
     * @param cargo_amount 货物数量
     * @return 出库订单页面
     */
    @RequestMapping("/outapprove")
    public String outApprove(
            @Param("order_num") String order_num,
            @Param("cargo_name") String cargo_name,
            @Param("cargo_amount") int cargo_amount,
            Model model
    ) {
        // 更新出库订单的状态
        outOrderService.updateOrderState(order_num, 1);
        // 根据出库货物名字和数量,更新仓库中的货物信息
        currentCargoService.delSomeCargo(cargo_name, cargo_amount);

        // 查询状态是未审核的所有出库订单
        List<Out_Order> out_orders = outOrderService.queryAllOrder();
        model.addAttribute("entryoutOrders", out_orders);
        // 跳转到出库订单展示页面
        return "managers/outOrder";
    }

    // 退货订单
    @RequestMapping("/Return")
    public String PageToReturn(Model model) {
        // 查询状态为未审核的退货订单,并展示
        List<Return_Order> return_orders = returnOrderService.queryAllOrder();
        model.addAttribute("returnOrders", return_orders);
        return "managers/returnOrder";
    }


    /**
     * 展示退货订单的详细内容
     *
     * @param order_num 退货订单号
     * @param model     数据传输
     * @return 退货订单详细内容页面
     */
    @RequestMapping("/returnOrderContent")
    public String returnOrderContent(
            @Param("order_num") String order_num,
            Model model
    ) {
        // 根据退货订单号查询对应的退货订单
        Return_Order return_order = returnOrderService.queryByOrderNum(order_num);
        // 根据退货订单对应的出库订单号查询对应的出库订单
        Out_Order out_order = outOrderService.queryByOrderNum(return_order.getOut_order_num());
        // 传数据
        model.addAttribute("outOrder", out_order);
        model.addAttribute("returnOrder", return_order);
        // 跳转到退货订单详细内容页面
        return "managers/returnOrderContent";
    }

    /**
     * 批准退货申请 处理流程
     *
     * @param outOrderNum 退货订单对应的出库订单
     * @param order_num   退货订单号
     * @return 处理结束后，返回到退货订单页面
     */
    @RequestMapping("/returnapprove")
    public String returnapprove(
            @Param("outOrderNum") String outOrderNum,
            @Param("order_num") String order_num,
            Model model
    ) {
        // 首先，根据出库订单号，查询出货订单
        Out_Order out_order = outOrderService.queryByOrderNum(outOrderNum);

        // 当查询到出库订单时
        if (out_order != null) {
            // 根据出库订单中的货物名，查询到相应的货物对象
            Current_Cargo current_cargo = currentCargoService.queryByCargoName(out_order.getCargo_name());

            // 当货物对象不为空时
            if (current_cargo != null) {


                // 因为是退货，所以增加货物的数量，增加货物的结果（是否成功）使用isSuccess来记录，如果失败
                // 那么结果就是0
                int isSuccess = currentCargoService.addCargoAmount(current_cargo.getCargo_name(), out_order.getCargo_amount());

                // 货物增加成功
                if (isSuccess != 0) {
                    // 增加完数量后，将退货订单的状态进行转换
                    returnOrderService.updateOrderState(order_num, 1);
                }

            }
        }
        // 处理完订单后，跳转到退货订单页面
        // 先查询库中还有的退货订单，然后跳转
        List<Return_Order> return_orders = returnOrderService.queryAllOrder();
        model.addAttribute("returnOrders", return_orders);
        return "managers/returnOrder";
    }
}
